﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
using 雷赛板卡上位机.Models;
using 雷赛板卡上位机.Services;
using ControlType = ServoDrivesLib.Model.ControlType;

namespace 雷赛板卡上位机.Attributes
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    internal class CardRangeAttribute : ValidationAttribute
    {
        private ControlType _controlType;
        private readonly double _max;
        private readonly double _min;

        public CardRangeAttribute(ControlType controlType, double max, double min)
        {
            _controlType = controlType;
            _max = max;
            _min = min;
        }
        public override bool IsValid(object? value)
        {
            if (ControlService.GetControlType() == _controlType)
            {
                if (value == null) return false;
                if (!double.TryParse(value.ToString(), out double val))
                    return false;
                if (val < _min) return false;
                if (val > _max) return false;
                return true;
            }
            return true;
        }
        public override string FormatErrorMessage(string name)
        {
            return string.Format(ErrorMessage, _max, _min);
        }
    }
}
